<template>
  <div class="goodsinfo_container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotuList" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{ goodsInfo.market_price }}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{ goodsInfo.sell_price }}</span>
          </p>
          <p>购买数量：
            <number-box :max="goodsInfo.stock_quantity" @getCount="getSelectedCounted"></number-box>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsNumber }}</p>
          <p>库存情况：{{ goodsInfo.stock_quantity }}</p>
          <p>上架时间：{{ goodsInfo.add_time }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">商品介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue";
import numberBox from "../subcomponents/goodsinfo_numbox.vue";

export default {
  data() {
    return {
      id: this.$route.params.id,
      pageindex: 1, // 商品列表第一页数据
      lunbotuList: [], // 轮播图列表
      goodsNumber: "",
      goodsInfo: {},
      ballFlag: false, // 小球的显示状态 -- 默认隐藏
      selectedCount: 1 // 保存用户选中的商品数量，默认，认为用户买一个
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsInfo1();
    this.getGoodsInfo2();
  },
  methods: {
    getLunbotu() {
      this.$http.get("getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.lunbotuList = result.body.message.goodsImages;
        }
      });
    },
    getGoodsInfo1() {
      this.$http.get("getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsNumber = result.body.message.goodsNumber;
        }
      });
    },
    getGoodsInfo2() {
      this.$http.get("getgoods?pageindex=" + this.pageindex).then(result => {
        if (result.body.status === 0) {
          this.goodsInfo = result.body.message[this.id - 1];
        }
      });
    },
    goDesc(id) {
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    goComment(id) {
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    addToShopCar() {
      // 小球添加到购物车
      this.ballFlag = !this.ballFlag;

      // 拼接出一个，要保持到 store 中 car 数组里的 商品信息对象
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsInfo.sell_price,
        selected: true
      };
      // 调用 store 中的 mutations 来将商品加入购物车
      this.$store.commit('addToCar', goodsinfo)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;

      // 获取 小球 在页面中的位置
      const ballPostition = this.$refs.ball.getBoundingClientRect();
      // 获取 徽标 在页面中的位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      el.style.transform =
        "translate(" +
        (badgePosition.left - ballPostition.left) +
        "px, " +
        (badgePosition.top - ballPostition.top) +
        "px)";
      el.style.transition = "all .5s cubic-bezier(0,-0.68,.96,.79)";

      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCounted(count) {
      this.selectedCount = count;
    }
  },
  components: {
    swiper,
    numberBox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo_container {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 410px;
    left: 150px;
  }
}
</style>
